﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BikeSystem.Models;
using BikeSystem.BLL;

namespace BikeUI
{
    public partial class FormOptions : BikeUI.FormBase
    {

        AccountManager accountManager = new AccountManager();
        public FormOptions()
        {
            InitializeComponent();

            cbFilterOptionType.Items.Add("ALL");
            cbFilterOptionType.Items.Add(ConstData.OPTION_BICYCLE_TYPE);
            cbFilterOptionType.Items.Add(ConstData.OPTION_BICYCLE_RATE_DAILY);
            cbFilterOptionType.Items.Add(ConstData.OPTION_BICYCLE_RATE_HOURLY);

            cbFilterOptionType.SelectedIndex = 0;

            cbOptType.Items.Add(ConstData.OPTION_BICYCLE_TYPE);
            cbOptType.Items.Add(ConstData.OPTION_BICYCLE_RATE_DAILY);
            cbOptType.Items.Add(ConstData.OPTION_BICYCLE_RATE_HOURLY);

            loadSystemOptions();


            UCButtonGroup.ButtonUpdate.Visible = false;

            UCButtonGroup.KeyComponent = txtOptCode;
            UCButtonGroup.EditableGroupBox = gbxOption;
            UCButtonGroup.BindingSource = systemOptionBindingSource;

            UCButtonGroup.EditableGroupBox.Enabled = false;
        }

        protected override void btnNew_Click(object sender, EventArgs e)
        {

            SystemOption systemOption = (SystemOption)systemOptionBindingSource.Current;
            systemOption.IsNew = true;
            systemOption.OptionCode = CommonUtils.genrateNextSerialNumber(ConstData.SYSTEM_NEXT_SERIALNUMBER_OPTION);

            txtOptCode.Text = systemOption.OptionCode;

        }


        protected override void btnSave_Click(object sender, EventArgs e)
        {

            errorProviderClear();
            txtAlphaNumericString_Validated(txtOptCode);
            txtAlphaNumericString_Validated(txtOptName);

            if (!UCButtonGroup.HasError)
            {
                SystemOption systemOption = (SystemOption)systemOptionBindingSource.Current;
                if (systemOption.IsNew)
                {
                    accountManager.InsertSystemOption(systemOption);
                }
                loadSystemOptions();

            }


        }

        protected override void btnDelete_Click(object sender, EventArgs e)
        {

            SystemOption systemOption = (SystemOption)systemOptionBindingSource.Current;

            if (!systemOption.IsNew)
            {
                accountManager.DeleteSystemOption(systemOption.OptionCode);
            }

            loadSystemOptions();
        }

        private void cbFilterOptionType_SelectedValueChanged(object sender, EventArgs e)
        {
            loadSystemOptions();
        }

        private void loadSystemOptions()
        {

            IList<SystemOption> systemOptions = accountManager.GetAllOptions();

            //update parent's SystemOptions;
            //((MainFormWindow)MdiParent).SystemOptions = systemOptions;

            if ("ALL".Equals(cbFilterOptionType.Text))
            {
                systemOptionBindingSource.DataSource = systemOptions;
            }
            else
            {
                systemOptionBindingSource.DataSource = systemOptions.Where(p => p.OptionType.Equals(cbFilterOptionType.Text)).ToList<SystemOption>();
            }



        }



    }
}
